package com.jiaquyun.jcyx.jetpack.api

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.entity.GoodsListResponse
import com.jiaquyun.jcyx.entity.GoodsRequest
import com.jiaquyun.jcyx.entity.HomeResponse
import com.jiaquyun.jcyx.base.BaseManager
import com.module.lemlin.http.HttpResponseData
import rxhttp.RxHttp
import rxhttp.await

class IndexRepository : BaseManager() {

    /**
     * 首页
     * */
    suspend fun index(): HomeResponse =
        RxHttp.get("/api/index/index")
            .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
            .await()

    suspend fun indexAndGoodsList(goodsRequest: GoodsRequest): HomeResponse {

        LogUtils.e("=====1==}")
        val home = RxHttp.get("/api/index/index")
            .add("shop_id", goodsRequest.shop_id)
            .await<HomeResponse>()
        LogUtils.e("=====1==${GsonUtils.toJson(home)}")
        val maps: Map<String, String?> =
            GsonUtils.fromJson<Map<String, String?>>(
                GsonUtils.toJson(goodsRequest),
                Map::class.java
            ).filter { it.value != null }
        val goods = RxHttp.get("/api/goods/goodsList")
            .addAll(maps)
            .await<GoodsListResponse>()
        return home.apply {
            this.data.goodsList = if (goods.data.size == 0)
                mutableListOf() else goods.data
        }
    }

    /**
     * 热卖商品
     * */
    suspend fun indexHotGoods(
        page: String, rows: String
    ): HttpResponseData = RxHttp.get("/api/index/hotGoods")
        .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
        .add("page", page)
        .add("rows", rows)
        .await()

    /**
     * 启动图
     * */
    suspend fun indexBootAd(): HttpResponseData =
        RxHttp.postForm("/api/index/bootAd")
            .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
            .await()

    /**
     * 获取轮播图
     * */
    suspend fun indexGetBanner(
        type: String
    ): HttpResponseData = RxHttp.postForm("/api/index/getBanner")
        .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
        .add("type", type)
        .await()

}